/*
 * Copyright (C) 2021 The Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.oushangfeng.pinneddemo.custom.categorylist;

import com.oushangfeng.pinneddemo.ResourceTable;
import com.oushangfeng.pinneddemo.datainterface.CategoryItemBase;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.Text;
import ohos.app.Context;

/**
 * StockListItem
 *
 * @since 2021-03-27
 * Created by oubowu on 2021/3/27 10:29
 * SingleList item which display a left text and a right text in the list container item.
 */
public class StockListItem implements CategoryItemBase {
    /**
     * SingleListItem type
     */
    public static final int ITEM_TYPE = 5;
    private static final int INT_2 = 2;
    private static final int INT_3 = 3;
    private static final int INT_4 = 4;
    private static final int INT_69 = 69;
    private static final int INT_183 = 183;
    private static final int INT_124 = 124;

    private final Context context;
    private String allText;
    private ShapeElement shapeElement = new ShapeElement();

    StockListItem(Context context, String allText) {
        this.context = context;
        this.allText = allText;
    }

    StockListItem(Context context, String allText, int type) {
        this.context = context;
        this.allText = allText;
    }

    /**
     * Set component content
     *
     * @param component the component to set
     */
    @Override
    public void bindComponent(Component component) {
        // item context
        String[] contents = allText.split(",");
        if (null != contents && INT_4 == contents.length) {
            Text leftText = (Text) component.findComponentById(ResourceTable.Id_left_text);
            leftText.setText(contents[0]);
            Text leftBottomText = (Text) component.findComponentById(ResourceTable.Id_left_bottom_text);
            leftBottomText.setText(contents[1]);
            Text centerText = (Text) component.findComponentById(ResourceTable.Id_center_text);
            centerText.setText(contents[INT_2]);
            Text rightText = (Text) component.findComponentById(ResourceTable.Id_right_text);
            rightText.setText(contents[INT_3]);
        } else {
            shapeElement.setRgbColor(new RgbColor(INT_69, INT_183, INT_124));
            DependentLayout dependentLayout = (DependentLayout) component.
                    findComponentById(ResourceTable.Id_stock_depend);
            dependentLayout.setBackground(shapeElement);
        }
    }

    /**
     * Create component
     *
     * @return component
     */
    @Override
    public Component createComponent() {
        return LayoutScatter.getInstance(context).parse(
                ResourceTable.Layout_stock_item, null, false);
    }

    /**
     * Get the item type of this category item
     *
     * @return category item type
     */
    @Override
    public int getItemType() {
        return ITEM_TYPE;
    }

    @Override
    public String getItem() {
        return allText;
    }

    @Override
    public void setItem(String value) {
        allText = value;
    }

    @Override
    public String getItemName() {
        return allText;
    }
}
